﻿using System;
using System.Collections.Generic;

namespace TLang.Ast
{
    using Values;

    public class Fun : Node
    {
        public List<Name> prams;
        public Node body;
        public Scope propertyForm;


        public Fun(List<Name> prams, Scope propertyForm, Node body, String file, int start, int end, int line, int col) : base(file, start, end, line, col)
        {
            this.prams = prams;
            this.propertyForm = propertyForm;     // unevaluated property form
            this.body = body;
        }


        public override Value Interp(Scope s)
        {
            // evaluate and cache the properties in the closure
            Scope properties = propertyForm == null ? null : Declare.evalProperties(propertyForm, s);
            return new Closure(this, properties, s);
        }

        public override Value Typecheck(Scope s)
        {
            // evaluate and cache the properties in the closure
            Scope properties = propertyForm == null ? null : Declare.typecheckProperties(propertyForm, s);
            FunType ft = new FunType(this, properties, s);
            TypeChecker.self.uncalled.Add(ft);
            return ft;
        }


        public override String ToString()
        {
            return "(" + Constants.FUN_KEYWORD + " (" + prams + ") " + body + ")";
        }
    }
}
